package com.jsmile.cloud.msgcenter.pay.listener;

import java.util.stream.Collectors;

import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.jsmile.cloud.msgcenter.pay.event.PayOrderSuccessEvent;
import com.jsmile.cloud.msgcenter.pay.model.PayOrderInfo;
import com.jsmile.cloud.tradecenter.api.feign.OrderServiceApi;
import com.jsmile.cloud.tradecenter.api.req.ReqPayOrder;

import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 订单变化--更新账户余额
 */
@Slf4j
@Component
public class PaySuccessListener {

    @Order
    @EventListener(PayOrderSuccessEvent.class)
    public void paySuccess(PayOrderSuccessEvent event) {
        log.info("支付成功更新订单状态：{}", event.getPayOrderInfos());
        SpringUtil.getBean(OrderServiceApi.class).paySuccess(ReqPayOrder.builder().payType(event.getPayOrderInfos().get(0).getPayType())
            .orderNos(event.getPayOrderInfos().stream().map(PayOrderInfo::getOrderNo).collect(Collectors.toList())).build());
    }

}
